#include "mutex.hpp"
#include <iostream>
#include <string>
#include <unistd.h>
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
int tickets = 10000;
void *getTickets(void *args)
{
    std::string username = static_cast<const char *>(args);
    while (true)
    {
        {
            LockGuard lockguard(&lock);
            if (tickets > 0)
            {
                usleep(1254);
                std::cout << username << " : 正在进行抢票 : " << tickets << std::endl;
                tickets--;
            }
            else
            {
                break;
            }
        }
        usleep(1000);
    }
    return nullptr;
}
int main()
{
    pthread_t t1, t2, t3, t4;
    pthread_create(&t1, nullptr, getTickets, (void *)"thread 1");
    pthread_create(&t2, nullptr, getTickets, (void *)"thread 2");
    pthread_create(&t3, nullptr, getTickets, (void *)"thread 3");
    pthread_create(&t4, nullptr, getTickets, (void *)"thread 4");
    pthread_join(t1, nullptr);
    pthread_join(t2, nullptr);
    pthread_join(t3, nullptr);
    pthread_join(t4, nullptr);
    return 0;
}